package com.siyu.tickets.action;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.siyu.common.page.Pagination;
import com.siyu.common.page.SimplePage;
import com.siyu.common.web.CookieUtils;
import com.siyu.tickets.entity.Partner;
import com.siyu.tickets.manager.PartnerMng;

/**
 * 供应商相关
 * 
 * @since 2013-7-11
 * @version 0.1
 * @author cuigx
 */
@Controller
public class PartnerAct {

	private static final Logger log = LoggerFactory.getLogger(PartnerAct.class);

	@RequestMapping("/partner/list")
	public String list(String name, Boolean enable, Integer pageNo, HttpServletRequest request, ModelMap model) {
		Pagination pagination = manager.getPage(name, enable, SimplePage.cpn(pageNo), CookieUtils.getPageSize(request));
		model.addAttribute("name", name);
		model.addAttribute("enable", enable);
		model.addAttribute("pagination", pagination);
		model.addAttribute("pageNo", pagination.getPageNo());
		return "partner/list";
	}

	@RequestMapping("/partner/edit")
	public String edit(Integer id, HttpServletRequest request, ModelMap model) {
		Partner partner = manager.findById(id);
		model.addAttribute("partner", partner);
		return "partner/edit";
	}

	@RequestMapping("/partner/update")
	public String update(Partner bean, HttpServletRequest request, ModelMap model) {
		manager.updatePartner(bean);
		return "redirect:list";
	}

	@RequestMapping("/partner/delete")
	public String delete(Integer[] ids, Integer pageNo, HttpServletRequest request, ModelMap model) {
		Partner[] beans = manager.deleteByIds(ids);
		for (Partner bean : beans) {
			log.info("delete Partner id={}", bean.getId());
		}
		return "redirect:list";
	}

	@RequestMapping("/partner/enable")
	public String enable(Integer[] ids, Boolean enable, Integer pageNo, HttpServletRequest request, ModelMap model) {
		manager.enableByIds(ids, enable);
		return "redirect:list";
	}

	@RequestMapping(value = "/register", method = RequestMethod.GET)
	public String register(HttpServletRequest request, ModelMap model) {
		return "partner/register";
	}

	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public String register(Partner bean, HttpServletRequest request, ModelMap model) {
		try {
			manager.registPartner(bean);
			return "partner/register_result";
		} catch (Exception e) {
			e.printStackTrace();
			model.addAttribute("message", e.getMessage());
			return "partner/register_result";
		}
	}

	@Autowired
	private PartnerMng manager;

}
